<?php

namespace app\home\lib;

class WeiXin
{
    private $appid;
    private $secret;

    public function __construct()
    {
        $this->appid = 'wx8d1aba3e4bf5ed6c';
        $this->secret = 'c6e69a670b92dc304d6e684503e7a8f5';
    }
    /**
     * 获取用户信息
     * @param [type] $code
     * @return void
     */
    public function jscode2session($code)
    {
        $data = curlGet("https://api.weixin.qq.com/sns/jscode2session?appid={$this->appid}&secret={$this->secret}&js_code={$code}&grant_type=authorization_code");
        if (!$data) {
            return false;
        }
        $data = json_decode($data, true);
        return $data;
    }

    public function getUserInfo($encryptData, $iv, $sessionKey)
    {
        if (strlen($sessionKey) != 24) {
            return self::IllegalAesKey;
        }
        $aesKey = base64_decode($sessionKey);
        if (strlen($iv) != 24) {
            return self::IllegalIv;
        }
        $aesIV = base64_decode($iv);
        $aesCipher = base64_decode($encryptData);
        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $dataObj = json_decode($result, true);
        if ($dataObj == NULL) {
            return self::IllegalBuffer;
        }
        return $dataObj;
    }
}